app.register.controller('rebateRegCtrl', function($scope, $http, $rootScope,
		$route, RebateRegSer, AppUtil, $routeParams) {
	
	loadFlag($routeParams.id,$scope);
	
	// 获取本地存储中的页码页数 tableID+page or size
	var page = AppUtil.GetLocalValue("rebateRegTable_Page");
	var size = AppUtil.GetLocalValue("rebateRegTable_Size");
	$scope.pagination = {
			totalItems :0,
		currentPage : page == undefined ? 1 : page,
		itemsPerPage : size == undefined ? 10 : size
	};
	
	$scope.SearchData = function() {
		$scope.postData = {};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		LoadList();
	};
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		// 表单查询参数
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, params,$scope.selected);
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		console.log($scope.postData.page);
		// 加载数据
		RebateRegSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.datas = response.data;
			setTimeout(check_table_tr, 500);
		});
	}

	$scope.OpenUrl = function(c) {
		location.href = "#/rebate/detail/" + c.entity_code + "/" + c.rebate_no
				+ "";
	};

	// 配置分页监听
	$scope.$watch('pagination.refresh', LoadList);

	// 加载查询信息
	AppUtil.LoadGroups();
	$http.get("common/departments").success(function(data) {
		$scope.departments = data;
	});
	$http.get("common/rebateStatus").success(function(data) {
		$scope.status_list = data;
	});
	/*$http.get("common/rebateTypes").success(function(data) {
		$scope.types = data;
	});*/
	$http.get("common/rebateWorkTypes").success(function(data) {
		$scope.btypes = data;
	});

	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};

	// 双击跳转明细页面
	$scope.showDetail = function(entity_code, rebate_no) {
		$("#myModal_detail").modal("show");
		var postData = {};
		postData.entity_code = entity_code;
		postData.rebate_no = rebate_no;
		// 加载数据方法

		AppUtil.loading();
		$http.post('rebate/get', postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.rebate = response.data.rebate;
			$scope.items = response.data.items;
		}).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});

	};

});

app.register.service('RebateRegSer', function($http, AppUtil) {

	// 查询数据
	this.list = function(postData) {
		AppUtil.loading();
		postData.start = (postData.page - 1 < 0 ? 0 : postData.page - 1)
				* postData.size;
		return $http.post('rebate/list', postData).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});
	};

});
